package com.webserver.controller;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.Article;
import com.webserver.vo.User;
import org.w3c.dom.ls.LSOutput;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class ArticleController {
    private static String articleTitle = "./articles/";

    static {
        File articleDir = new File(articleTitle);
        if (!articleDir.exists()) {
            articleDir.mkdirs();
        }
    }


    public void writeArticle(HttpRequest request, HttpResponse response) {
        String title = request.getParameter("title");
        String author=request.getParameter("author");
        String content = request.getParameter("content");
        System.out.println(title + "," + author +"," + content);

        if (title == null || author == null|| content == null) {
            response.setEntity(new File("./webapps/myweb/article_fail.html"));
            return;
        }
//        File artileFile =new File(articleTitle+title+".obj");
        Article article=new Article(title,author,content);

        try (
                ObjectOutputStream oos = new ObjectOutputStream(
                        new FileOutputStream(articleTitle+title+ ".obj")
                )){
            oos.writeObject(article);
            response.setEntity(new File("./webapps/myweb/article_success.html"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("文章发布完毕!");
    }




}
